package top.aylan.org.ctrl.admin;

import com.alibaba.fastjson.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import top.aylan.org.entity.Banner;
import top.aylan.org.service.BannerService;
import top.aylan.org.tools.Directory;
import top.aylan.org.tools.FileUploader;
import top.aylan.org.tools.Pager;
import top.aylan.org.tools.Result;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping("/admin/slider")
public class AdminSliderController {

    @Resource
    BannerService bannerService;

    @RequestMapping(value = "/list")
    public String index(ModelMap modelMap,
                        @RequestParam(value = "page",defaultValue = "1")Integer page){
        Pager pager = new Pager();
        Banner banner = new Banner();

        pager.setCondition(banner);
        pager.setCurrentPage(page);

        try {
            pager.setList(bannerService.selectBannerByPage(pager));
            modelMap.addAttribute("pager",pager);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return "admin/slider";
    }


    @RequestMapping("/add")
    public String add(ModelMap modelMap){

        return "admin/slider-add";
    }

    @RequestMapping("/edit")
    public String edit(@RequestParam(defaultValue = "0",value = "id")int id,ModelMap modelMap){

        try {
            modelMap.addAttribute("data",bannerService.findById(id));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "admin/slider-edit";
    }

    @RequestMapping(value = "/save")
    public String save(@RequestParam(value = "title",defaultValue = "title")String title,
                       @RequestParam(value = "description",defaultValue = "")String description,
                       @RequestParam(value = "id",defaultValue = "0") Integer id,
                       @RequestParam(value = "content",defaultValue = "") String linkUrl,
                       MultipartFile file,
                       HttpServletRequest request
    ){

        byte status = 0;
        if(StringUtils.isEmpty(request.getParameter("status")) && "on".equals(request.getParameter("status"))){
            status = 1;
        }
        Banner banner = new Banner();
        banner.setTitle(title);
        banner.setStatus(status);
        banner.setDescription(description);
        banner.setLinkUrl(linkUrl);

        FileUploader fileUploader = new FileUploader();
        Result res = fileUploader.upload(file, Directory.IMAGE);
        if(res.getCode() == 1){
            banner.setPath(res.getData().get("path").toString());
        }
        try {
            if(id == 0){
                bannerService.save(banner);
            }else{
                bannerService.updateById(id,banner);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return "redirect:/admin/slider/list";
    }


    /*
     *
     */
    @RequestMapping(value = "/state",method = RequestMethod.POST)
    @ResponseBody
    public JSONObject state(@RequestParam(value = "id",defaultValue = "0")int id,
                            @RequestParam(value = "state",defaultValue = "0") byte status
    ){
        JSONObject object = new JSONObject();
        Banner banner = new Banner();
        banner.setId(id);
        banner.setStatus(status);
        try {
            bannerService.updateById(id,banner);
            object.put("code",1);
        } catch (Exception e) {
            object.put("code",-1);
            object.put("msg",e.getMessage());
        }
        return object;
    }

    @RequestMapping("/delete")
    public String delete(@RequestParam(value = "id",defaultValue = "0") int id){

        try {
            bannerService.delete(id);
            return "redirect:/admin/slider/list";
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "admin/slider";
    }

}
